#ifndef __WIN_HPP_
#define __WIN_HPP_

namespace {
  sdl::Window::Wrapped win_;
  sdl::Renderer::Wrapped ren_;
  sdl::Window::Wrapped createWin() {
    return sdl::Window::create("sdl++ window"
      , SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED
      , 800, 600, sdl::Window::Flags::SDL_WINDOW_SHOWN);
  }
  sdl::Renderer::Wrapped createRenderer(sdl::Window::Wrapped &win) {
    //sdl::Renderer::getNumDrivers();
    return win->createRenderer(-1, sdl::Renderer::Flags::SDL_RENDERER_ACCELERATED);
  }
  void windowEvent(sdl::event::WindowEvent evt) {
    if (evt.windowID != win_->getID()) return;

    if (evt.event == sdl::event::WindowEventID::SDL_WINDOWEVENT_EXPOSED) {
      auto sz = win_->getSize();
      sdl::Rect rc(sz);
      ren_->clear();
      ren_->setDrawColor(255, 255, 0, 255);
      ren_->fillRect(&rc);
      ren_->present();
    }
  }
}

void win_create() {
  win_ = createWin();
  if (!win_) return;
  ren_ = createRenderer(win_);
  if (!ren_) return;
}

void win_handle(sdl::event::Event &evt) {
  if (evt.type == sdl::event::Type::SDL_WINDOWEVENT) {
    windowEvent(evt.window);
  }
}

void win_destroy() {
  if (win_) win_->destroy();
  if (ren_) ren_->destroy();
}

#endif // __WIN_HPP_